package com.pan.sign_in.service.impl;

import com.pan.sign_in.POJO.TO.CallNameTO;
import com.pan.sign_in.POJO.TO.LoginTO;
import com.pan.sign_in.POJO.VO.CallNameDataVO;
import com.pan.sign_in.dao.*;
import com.pan.sign_in.entity.*;
import com.pan.sign_in.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private CallNameDao callNameDao;

    @Autowired
    private StudentCourseDao studentCourseDao;

    @Autowired
    private SignRecordDao signRecordDao;

    @Override
    public Teacher Login(LoginTO loginTO) {
        Teacher teacher = teacherDao.SelectTeacherByIdPasswd(loginTO);
        if (teacher==null) {
            System.out.println("账户名或密码错误");
        }else {
            System.out.println("登录成功");
        }
        return teacher;
    }

    @Override
    public void AddCourse(Course course) {
        courseDao.AddCourse(course);
    }

    @Override
    public void CallName(CallNameTO callNameTO) {
        CallName callName = new CallName();

        callName.setTId(callNameTO.getTId());
        callName.setCId(callNameTO.getCId());
        callName.setCode(callNameTO.getCode());
        callName.setDateTime(new Date());

        callNameDao.createCallName(callName);
    }


    @Override
    public List<CallName> CourseCallNameRecord(Long cId) {
        List<CallName> records = callNameDao.selectRecoderByCId( cId);
        return records;
    }

    @Override
    public CallNameDataVO getCourseCallNameData(Long CallNameRecordId) {
        CallNameDataVO callNameDataVO = new CallNameDataVO();

        CallName callName = callNameDao.selectById(CallNameRecordId);

        callNameDataVO.setTId(callName.getTId());
        callNameDataVO.setCId(callName.getCId());
        callNameDataVO.setCallrecordId(callName.getId());

        //老师名字
        Teacher teacher = teacherDao.selectTeacherById(callName.getTId());
        callNameDataVO.setTName(teacher.getUname());

        //课程名字
        Course course = courseDao.selectCourseById(callName.getCId());

        callNameDataVO.setDateTime(callName.getDateTime());

        //课程人数
        List<StudentCourse> courseStudents = studentCourseDao.selectRecordByCId(course.getId());
        callNameDataVO.setCourseNum(courseStudents.size());

        List<SignRecord> signRecords = signRecordDao.getRecordByCallNameId(callName.getId());
        callNameDataVO.setCallName(signRecords.size());

        return callNameDataVO;
    }
}
